package cz.muni.fi.pa165.dundd.web;

import cz.muni.fi.pa165.dundd.dto.HeroRoleDTO;
import cz.muni.fi.pa165.dundd.service.HeroRoleService;
import java.util.List;
import net.sourceforge.stripes.action.*;
import net.sourceforge.stripes.controller.LifecycleStage;
import net.sourceforge.stripes.integration.spring.SpringBean;
import net.sourceforge.stripes.validation.Validate;
import net.sourceforge.stripes.validation.ValidateNestedProperties;
import net.sourceforge.stripes.validation.ValidationErrorHandler;
import net.sourceforge.stripes.validation.ValidationErrors;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 *
 * @author xkonval, xdemocko
 */
@UrlBinding("/roles/{$event}/{heroRole.id}")
public class RoleActionBean extends BaseActionBean implements ValidationErrorHandler {

    final static Logger log = LoggerFactory.getLogger(RoleActionBean.class);
    @SpringBean
    HeroRoleService heroRoleService;
    private List<HeroRoleDTO> roles;
    @ValidateNestedProperties(value = {
        @Validate(on = {"add", "save"}, field = "name", required = true),
        @Validate(on = {"add", "save"}, field = "description", required = true)
    })
    private HeroRoleDTO heroRole;

    public HeroRoleDTO getHeroRole() {
        return heroRole;
    }

    public void setHeroRole(HeroRoleDTO role) {
        this.heroRole = role;
    }

    public Resolution add() {
        heroRoleService.createRole(heroRole);
        getContext().getMessages().add(new LocalizableMessage("role.add.message", escapeHTML(heroRole.getName()), escapeHTML(heroRole.getDescription())));
        return new RedirectResolution(this.getClass(), "list");
    }

    @DefaultHandler
    public Resolution list() {
        roles = heroRoleService.retrieveAllRoles();
        return new ForwardResolution("/role/list.jsp");
    }

    public List<HeroRoleDTO> getRoles() {
        return roles;
    }

    public Resolution delete() {
        //only id is filled by the form
        heroRole = heroRoleService.retrieveRoleById(heroRole.getId());
        System.out.println(heroRole.getId());
        heroRoleService.deleteRole(heroRole);
        getContext().getMessages().add(new LocalizableMessage("role.delete.message",
                escapeHTML(heroRole.getName()), escapeHTML(heroRole.getDescription())));
        return new RedirectResolution(this.getClass(), "list");
    }

    @Override
    public Resolution handleValidationErrors(ValidationErrors errors) throws Exception {
        //fill up the data for the table if validation errors occured
        //return null to let the event handling continue
        roles= heroRoleService.retrieveAllRoles();
        return null;
    }

    @Before(stages = LifecycleStage.BindingAndValidation, on = {"edit", "save"})
    public void loadHeroRoleFromDatabase() {
        String ids = getContext().getRequest().getParameter("heroRole.id");
        if (ids == null) {
            return;
        }
        heroRole = heroRoleService.retrieveRoleById(Long.parseLong(ids));
    }

    public Resolution edit() {
        return new ForwardResolution("/role/edit.jsp");
    }

    public Resolution save() {
        heroRoleService.updateRole(heroRole);
        return new RedirectResolution(this.getClass(), "list");
    }
}
